<template>
  <div>
    <el-card class="box-card">
      <div class="head-top">
        <Bread first="文章管理" second="文章分类"></Bread>
        <el-button
          size="medium"
          type="primary"
          plain
          @click="dialogFormVisible = true"
          >添加分类</el-button
        >
      </div>

      <el-table :data="$store.state.article.artlist" border style="width: 100%">
        <el-table-column label="序号" prop="id" width="50"> </el-table-column>
        <el-table-column label="分类名称" width="180" prop="cate_name">
        </el-table-column>
        <el-table-column label="分类别称" prop="cate_alias"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 添加按钮 -->
      <!-- 获取文章分类 -->
      <!-- 渲染文章分类到页面 -->
    </el-card>
    <!-- 添加分类的弹框 -->
    <!--
      1. 添加表单
      2. 绑定表单
      3. 定义表单规则
      4. 关闭弹框事件
     -->
    <!--
       1. 添加文章分类
       2. 预校验通过
       3. 添加分类
          1. 添加成功
          1). 提示信息,关闭对话框
          2). 刷新页面,重新调用函数
          2.添加失败
          1). 提示信息
      -->
    <el-dialog
      title="添加文章分类"
      :visible.sync="dialogFormVisible"
      width="30%"
      @close="closeFn"
    >
      <el-form :model="form" :rules="addRules" ref="addForm">
        <el-form-item label="分类名称" label-width="80px" prop="cate_name">
          <el-input v-model="form.cate_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="分类别称" label-width="80px" prop="cate_alias">
          <el-input v-model="form.cate_alias" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="sureFn()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import { artadd } from '../../api/art'
export default {
  name: '',
  data () {
    return {
      dialogFormVisible: false,
      form: {
        cate_name: '',
        cate_alias: ''
      },
      // 添加的表单验证
      addRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          {
            pattern: /^\S{1,10}$/,
            message: '分类名称必须是1-10位的非空字符串',
            trigger: 'blur'
          }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: '分类别名必须是1-15位的字母数字',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created () {
    this.artGet()
  },
  computed: {},
  methods: {
    ...mapActions('article', ['artGet']),
    closeFn () {
      this.$refs.addForm.resetFields()
    },
    sureFn () {
      this.$refs.addForm.validate(async (valid) => {
        if (valid) {
          const { data } = await artadd(this.form)
          console.log(data)
          this.artGet()
          this.dialogFormVisible = false
          if (data.code === 0) {
            this.$message({
              message: data.message,
              type: 'success'
            })
          } else {
            this.$message({
              message: data.message,
              type: 'error'
            })
          }
        } else {
          return false
        }
      })
    },
    handleEdit (index, row) {
      console.log(index, row)
    },
    handleDelete (index, row) {
      console.log(index, row)
    }
  }
}
</script>
<style lang='less'  scoped>
.head-top {
  display: flex;
  justify-content: space-between;

  .el-button {
    margin: 30px 0 20px 0;
  }
}
</style>
